package com.paradise.phone.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.paradise.phone.R;

import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Sisyphus on 2017/9/24.
 */

public class DataBaseHelper extends SQLiteOpenHelper {

    public static final String CREATE_CONTACTS = "create table Contact ("
            + "id integer primary key autoincrement, "
            + "remarks text, "
            + "number text, "
            + "create_time text, "
            + "pinyin text)";

    private static DataBaseHelper dbHelper;

    private static Context mContext;

    private DataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public static DataBaseHelper instance(Context context){
        mContext = context;
        if (dbHelper == null){
            synchronized (DataBaseHelper.class){
                if (dbHelper == null){
                    dbHelper = new DataBaseHelper(context, "contacts", null, 2);
                }
            }
        }
        return dbHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_CONTACTS);
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        // 插入一个默认测试会议室账号
        String sql = null;
        try {
            sql = "insert into Contact(remarks, number, create_time, pinyin) select '" +
                    ""+mContext.getString(R.string.test_name)+"','" +mContext.getString(R.string.test_num)+"', '"+createTime+"'," +
                    "'"+new PinyinUtils().hanzi2Pinyin(mContext.getString(R.string.test_name), "", PinyinUtils.Type.FIRSTUPPER)+"'"+
                    " where not exists (select * from Contact where number='"+mContext.getString(R.string.test_num)+"')";
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch (oldVersion){
            case 1:
                db.execSQL("alter table Contact add column pinyin text");
                Cursor cursor = db.rawQuery("select * from Contact", null);
                while (cursor.moveToNext()){
                    int index = cursor.getColumnIndex("remarks");
                    String remarks = cursor.getString(index);
                    try {
                        String pinyin = new PinyinUtils().hanzi2Pinyin(remarks, "", PinyinUtils.Type.FIRSTUPPER);
                        db.execSQL("update Contact set pinyin = '"+pinyin+"' where id = "+cursor.getInt(0));
                    } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                        badHanyuPinyinOutputFormatCombination.printStackTrace();
                        Log.d("flag->", "DataBaseHelper.java(onUpgrade:73)->"+badHanyuPinyinOutputFormatCombination);
                    }
                }
                break;
        }
    }
}
